<?php

/**
 * 数据签名认证
 * @param  array  $data 被认证的数据
 * @return string       签名
 */
function data_auth_sign($data,$screct) {
    //数据类型检测
    if(!is_array($data)){
        $data = (array)$data;
    }
    ksort($data); //排序
    $code = http_build_query($data,$screct); //url编码并生成query字符串
    $sign = sha1($code); //生成签名
    return $sign;
}


//上传文件黑名单过滤
function upload_replace($str){
	$farr = ["/php|php3|php4|php5|phtml|pht|/is"];
	$str = preg_replace($farr,'',$str);
	return $str;
}



//根据键名获取键值
function getItemVal($val,$item_config){
	if(!is_null($val)){
		$str = '';
		foreach(explode(',',$val) as $v){
			foreach(json_decode($item_config,true) as $m){
				if($v == $m['val']){
					$str .= $m['key'].',';
				}
			}
		}
		return rtrim($str,',');
	}
}

//根据键值获取键名
function getValByKey($val,$item_config){
	if($val){
		$str = '';
		foreach(explode(',',$val) as $v){
			foreach(json_decode($item_config,true) as $m){
				if($v == $m['key']){
					$str .= $m['val'].',';
				}
			}
		}
		return rtrim($str,',');
	}
}

//无限极分类转为带有 children的树形select结构
function _generateSelectTree ($data, $pid = 0) {
	$tree = [];
	if ($data && is_array($data)) {
		foreach($data as $v) {
			if($v['pid'] == $pid) {
				$tree[] = [
					'key' => $v['key'],
					'val' => $v['val'],
					'children' => _generateSelectTree($data, $v['val']),
				];
			}
		}
	}
	return $tree;
}

//无限极分类转为带有 children的树形list表格结构
function _generateListTree ($data, $pid = 0,$config=[]) {
	$tree = [];
	if ($data && is_array($data)) {
		foreach($data as $v) {
			if($v[$config[1]] == $pid) {
				$tree[] = array_merge($v,['children' => _generateListTree($data, $v[$config[0]],$config)]);
			}
		}
	}
	return $tree;
}


//获取键值对信息
function getItemData($data){
	$str = in_array(json_encode(array_values($data)),['[]','[[]]']) ? '' : json_encode(array_values($data),JSON_UNESCAPED_UNICODE);
	return $str;
}



/**
 * tp官方数组查询方法废弃，数组转化为现有支持的查询方法
 * @param array $data 原始查询条件
 * @return array
 */
//查询格式化
function formatWhere($query,$where){
	foreach($where as $k=>$v){
		if(is_array($v)){
			if($v[1]){
				switch($v[0]){
					case 'like';
						$query = $query->whereLike($k,'%'.$v[1].'%');
					break;
					
					case 'not like';
						$query = $query->whereNotLike($k,$v[1]);
					break;
					
					case 'in';
						$query = $query->whereIn($k,$v[1]);
					break;
					
					case 'not in';
						$query = $query->whereNotIn($k,$v[1]);
					break;
					
					case 'between';
						$query = $query->whereBetween($k,$v[1]);
					break;
					
					case 'is null';
						$query = $query->whereNull($k);
					break;
					
					case 'not null';
						$query = $query->whereNotNull($k);
					break;
					
					case '<>';
						$query = $query->where($k,'<>',$v[1]);
					break;
					
					case '>';
						$query = $query->where($k,'>',$v[1]);
					break;
					
					case '<';
						$query = $query->where($k,'<',$v[1]);
					break;
					
					case '>=';
						$query = $query->where($k,'>=',$v[1]);
					break;
					
					case '<=';
						$query = $query->where($k,'<=',$v[1]);
					break;
					
					case 'or';
						$query = $query->orWhere($k,$v[2],$v[1]);
					break;
					
					case 'find in set';
						$query = $query->whereFindInSet($k,$v[1]);
					break;
					
					case 'exp';
						$query = $query->whereExp($k,$v[1]);
					break;
				}
			}
		}else{
			if((string)$v != null){
				$query = $query->where($k,$v);
			}
		}
	}
		
	return $query;
}

